package com.yimi.yinhepda.views.main.function_module;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;

import com.yimi.yinhepda.base.BaseFragment;
import com.yimi.yinhepda.MainActivity;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.db.dao.FuncBtnTableDao;
import com.yimi.yinhepda.db.table.FuncBtnTable;
import com.yimi.yinhepda.utils.Common;
import com.yimi.yinhepda.utils.ToastUtil;
import com.yimi.yinhepda.views.check_balance.CheckBalanceActivity;
import com.yimi.yinhepda.views.createPackage_module.CreatePackageMangerActivity;
import com.yimi.yinhepda.views.dispatch_module.DispatchBackScanActivity;
import com.yimi.yinhepda.views.dispatch_module.DispatchTaskActivity;
import com.yimi.yinhepda.views.exceptionsUpload_module.ExceptionUploadActivity;
import com.yimi.yinhepda.views.exceptionsUpload_module.ProblemBillUploadActivity;
import com.yimi.yinhepda.views.exceptionsUpload_module.WithoutTitleBillActivity;
import com.yimi.yinhepda.views.kua_yue_spotGoods.KuaYueTaskActivity;
import com.yimi.yinhepda.views.lan_huo_scan.LanHuoScanActivity;
import com.yimi.yinhepda.views.sign_manager.SelectBreakCardTypeActivity;
import com.yimi.yinhepda.views.sort_module.SortingActivity;
import com.yimi.yinhepda.views.stockCheck_module.CheckTaskActivity;
import com.yimi.yinhepda.views.stockCheck_module.CheckTaskCreateActivity;
import com.yimi.yinhepda.views.stockCheck_module.DifferReportActivity;
import com.yimi.yinhepda.views.stockCheck_module.StockInLocalDeptActivity;
import com.yimi.yinhepda.views.unload_module.CreateUnloadTaskActivity;
import com.yimi.yinhepda.views.unload_module.palletbind.ForkliftUnBindActivity;
import com.yimi.yinhepda.views.unload_module.palletbind.SingleUnBindActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author: lj
 * Date: 2019/9/19 15:52
 * Description:首页
 */
public class FunctionFragment extends BaseFragment {

    /**
     * 创建盘点
     */
    @Bind(R.id.ll_create_check)
    LinearLayout llCreateChek;
    /**
     * 盘点列表
     */
    @Bind(R.id.ll_check_list)
    LinearLayout llCheckList;
    /**
     * 盘点差异
     */
    @Bind(R.id.ll_check_diff)
    LinearLayout llCheckDiff;
    /**
     * 问题件上报
     */
    @Bind(R.id.ll_problem_report)
    LinearLayout llProblemReport;
    /**
     * 无头件上报
     */
    @Bind(R.id.ll_nohead_report)
    LinearLayout llNoheadReport;
    /**
     * 复称检查
     */
    @Bind(R.id.ll_check_balance)
    LinearLayout llCheckBalance;
    /**
     * 配送任务
     */
    @Bind(R.id.ll_dispatch_task)
    LinearLayout llDispatchTask;
    /**
     * 配送退单
     */
    @Bind(R.id.ll_dispatch_back)
    LinearLayout llDispatchBack;
    /**
     * 叉车分拣
     */
    @Bind(R.id.ll_forklift_sort)
    LinearLayout llForkliftSort;
    /**
     * 单独绑定
     */
    @Bind(R.id.ll_single_bind)
    LinearLayout llSingleBind;
    /**
     * 运单入库
     */
    @Bind(R.id.ll_waybill_stock)
    LinearLayout llWaybillStock;
    /**
     * 场内分拣
     */
    @Bind(R.id.ll_in_site_sort)
    LinearLayout llInSiteSort;
    /**
     * 旧卸车扫描
     */
    @Bind(R.id.ll_old_unload_scan)
    LinearLayout llOldUnloadScan;
    /**
     * 揽货扫描
     */
    @Bind(R.id.ll_lan_huo_scan)
    LinearLayout llLanHuoScan;
    /**
     * 异常管理
     */
    @Bind(R.id.ll_exception_manage)
    LinearLayout llExceptionManage;
    /**
     * 建包管理
     */
    @Bind(R.id.ll_create_package_manage)
    LinearLayout llCraetePackageManage;
    /**
     * 移库倒货
     */
    @Bind(R.id.ll_move_stock)
    LinearLayout llMoveStock;
    /**
     * 签到管理
     */
    @Bind(R.id.ll_sign_manage)
    LinearLayout llSignManage;
    /**
     * 分拨自提
     */
    @Bind(R.id.ll_fenbo_ziti)
    LinearLayout llFenBoZiTi;
    /**
     * 跨越点货
     */
    @Bind(R.id.ll_kuaYue_spotGoods)
    LinearLayout llKuaYueSpotGoods;

    private List<FuncBtnTable> mList;

    @Override
    public String getPageName() {
        return "功能页";
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragmen_function;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //注册事件
        EventBus.getDefault().register(this);

        int deptType = sharedPreferUtil.getIntValue(Common.DEPT_TYPE);
        if (deptType == 5 || deptType == 6) {
            //分拨屏蔽签到管理、揽货扫描
            llSignManage.setEnabled(false);
            llLanHuoScan.setEnabled(false);
            llFenBoZiTi.setEnabled(true);
            llKuaYueSpotGoods.setEnabled(true);
        } else {
            llSignManage.setEnabled(true);
            llLanHuoScan.setEnabled(true);
            llFenBoZiTi.setEnabled(false);
            llKuaYueSpotGoods.setEnabled(false);
        }

        //隐藏常用功能按钮
        invisibleFuncBtn();
    }

    @OnClick({R.id.ll_create_check, R.id.ll_check_list, R.id.ll_check_diff, R.id.ll_problem_report, R.id.ll_nohead_report,
            R.id.ll_check_balance, R.id.ll_dispatch_task, R.id.ll_dispatch_back, R.id.ll_forklift_sort, R.id.ll_single_bind,
            R.id.ll_waybill_stock, R.id.ll_in_site_sort, R.id.ll_old_unload_scan, R.id.ll_lan_huo_scan, R.id.ll_kuaYue_spotGoods,
            R.id.ll_exception_manage, R.id.ll_create_package_manage, R.id.ll_move_stock, R.id.ll_sign_manage, R.id.ll_fenbo_ziti})
    @Override
    protected void onClickBtn(View v) {
        super.onClickBtn(v);
        switch (v.getId()) {
            case R.id.ll_create_check:
                //创建盘点单
                toActivity(CheckTaskCreateActivity.class);
                break;
            case R.id.ll_check_list:
                //盘点列表
                toActivity(CheckTaskActivity.class);
                break;
            case R.id.ll_check_diff:
                //盘点差异
                toActivity(DifferReportActivity.class);
                break;
            case R.id.ll_problem_report:
                //问题件上报
                toActivity(ProblemBillUploadActivity.class);
                break;
            case R.id.ll_nohead_report:
                //无头件上报
                toActivity(WithoutTitleBillActivity.class);
                break;
            case R.id.ll_check_balance:
                //复称检查
                if (sharedPreferUtil.getOfflineMode()) {
                    ToastUtil.showToast(getString(R.string.no_use_in_offline));
                    return;
                }
                //复秤检查
                Intent intent = new Intent(getContext(), CheckBalanceActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("iService", MainActivity.iService);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.ll_dispatch_task:
                //配送任务
                toActivity(DispatchTaskActivity.class);
                break;
            case R.id.ll_dispatch_back:
                //配送退单
                toActivity(DispatchBackScanActivity.class);
                break;
            case R.id.ll_forklift_sort:
                //叉车分拣
                toActivity(ForkliftUnBindActivity.class);
                break;
            case R.id.ll_single_bind:
                //单独绑定
                toActivity(SingleUnBindActivity.class);
                break;
            case R.id.ll_waybill_stock:
                //运单入库
                toActivity(StockInLocalDeptActivity.class);
                break;
            case R.id.ll_in_site_sort:
                //场内分拣
                toActivity(SortingActivity.class);
                break;
            case R.id.ll_old_unload_scan:
                //旧卸车扫描
                if (sharedPreferUtil.getOfflineMode()) {
                    ToastUtil.showToast(getString(R.string.no_use_in_offline));
                    return;
                }
                toActivity(CreateUnloadTaskActivity.class);
                break;
            case R.id.ll_lan_huo_scan:
                //揽货扫描
                toActivity(LanHuoScanActivity.class);
                break;
            case R.id.ll_exception_manage:
                //异常管理
                toActivity(ExceptionUploadActivity.class);
                break;
            case R.id.ll_create_package_manage:
                //建包管理
                toActivity(CreatePackageMangerActivity.class);
                break;
            case R.id.ll_move_stock:
                //移库倒货
                queryPlaceRequest();
                break;
            case R.id.ll_sign_manage:
                //签到管理
                toActivity(SelectBreakCardTypeActivity.class);
                break;
            case R.id.ll_fenbo_ziti:
                //分拨自提
                goToFenBoZiTi();
                break;
            case R.id.ll_kuaYue_spotGoods:
                //跨越点货
                toActivity(KuaYueTaskActivity.class);
                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //取消注册事件
        EventBus.getDefault().unregister(this);
    }

    /**
     * 隐藏常用功能按钮
     */
    private void invisibleFuncBtn() {

        llCreateChek.setVisibility(View.VISIBLE);
        llCheckBalance.setVisibility(View.VISIBLE);
        llForkliftSort.setVisibility(View.VISIBLE);
        llDispatchTask.setVisibility(View.VISIBLE);
        llCheckList.setVisibility(View.VISIBLE);
        llCheckDiff.setVisibility(View.VISIBLE);
        llProblemReport.setVisibility(View.VISIBLE);
        llNoheadReport.setVisibility(View.VISIBLE);
        llDispatchBack.setVisibility(View.VISIBLE);
        llSingleBind.setVisibility(View.VISIBLE);
        llWaybillStock.setVisibility(View.VISIBLE);
        llInSiteSort.setVisibility(View.VISIBLE);
        llOldUnloadScan.setVisibility(View.VISIBLE);
        llLanHuoScan.setVisibility(View.VISIBLE);
        llExceptionManage.setVisibility(View.VISIBLE);
        llCraetePackageManage.setVisibility(View.VISIBLE);
        llMoveStock.setVisibility(View.VISIBLE);
        llSignManage.setVisibility(View.VISIBLE);
        llFenBoZiTi.setVisibility(View.VISIBLE);
        llKuaYueSpotGoods.setVisibility(View.VISIBLE);

        Observable.create(new Observable.OnSubscribe<Void>() {
            @Override
            public void call(Subscriber<? super Void> subscriber) {
                mList = new FuncBtnTableDao().findByCommId(1);
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Void>() {
            @Override
            public void onCompleted() {
                if (mList != null) {
                    for (FuncBtnTable item : mList) {
                        if (item.getNameCode().equals(Common.FUNC_CJPD)) {
                            //创建盘点
                            llCreateChek.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_FCJC)) {
                            //复称检查
                            llCheckBalance.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_CCFJ)) {
                            //叉车分拣
                            llForkliftSort.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_PSRW)) {
                            //配送任务
                            llDispatchTask.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_PDLB)) {
                            //盘点列表
                            llCheckList.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_PDCY)) {
                            //盘点差异
                            llCheckDiff.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_WENTJSB)) {
                            //问题件上报
                            llProblemReport.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_WUTJSB)) {
                            //无头件上报
                            llNoheadReport.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_PSTD)) {
                            //配送退单
                            llDispatchBack.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_DDBD)) {
                            //单独绑定
                            llSingleBind.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_YDRK)) {
                            //运单入库
                            llWaybillStock.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_CNFJ)) {
                            //场内分拣
                            llInSiteSort.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_JXCSM)) {
                            //旧卸车扫描
                            llOldUnloadScan.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_LHSM)) {
                            //揽货扫描
                            llLanHuoScan.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_YCGL)) {
                            //异常管理
                            llExceptionManage.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_JBGL)) {
                            //建包管理
                            llCraetePackageManage.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_YKDH)) {
                            //移库倒货
                            llMoveStock.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_QDGL)) {
                            //签到管理
                            llSignManage.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_FBZT)) {
                            //分拨自提
                            llFenBoZiTi.setVisibility(View.GONE);
                        } else if (item.getNameCode().equals(Common.FUNC_KYDH)) {
                            //同行卸车
                            llKuaYueSpotGoods.setVisibility(View.GONE);
                        }
                    }
                }
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onNext(Void aVoid) {
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainEventBus(String event) {
        //隐藏常用功能按钮
        invisibleFuncBtn();
    }
}
